package com.igroupes.ovineherd.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.igroupes.ovineherd.constant.CacheName;
import com.igroupes.ovineherd.entity.SystemConfig;
import com.igroupes.ovineherd.enums.EnableEnum;
import com.igroupes.ovineherd.enums.SystemConfigKey;
import com.igroupes.ovineherd.raw.service.SystemConfigService;
import com.igroupes.ovineherd.service.SystemConfigManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SystemConfigManagerImpl implements SystemConfigManager {
    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private SystemConfigManager self;

    @Cacheable(value = CacheName.SYSTEM_CONFIG, key = "#category", unless = "#result != null")
    @Override
    public Map<String, String> getConfig(String category) {
        if (StringUtils.isBlank(category)) {
            log.warn("查询的系统配置分类为空");
            return null;
        }
        SystemConfig systemConfigFind = new SystemConfig();
        systemConfigFind.setCategory(category);
        systemConfigFind.setEnabled(EnableEnum.ENABLE.getCode());
        List<SystemConfig> systemConfigList = systemConfigService.list(new QueryWrapper<>(systemConfigFind));

        if (CollectionUtils.isNotEmpty(systemConfigList)) {
            return systemConfigList.stream().collect(Collectors.toMap(SystemConfig::getKey, SystemConfig::getValue));
        }

        return null;
    }

    @Override
    public String getString(String category, String key) {
        return self.getConfig(category).get(key);
    }

    @Override
    public String getString(SystemConfigKey key) {
        return getString(key.getCategory(), key.getKey());
    }

    @Override
    public String getStringOrDefault(String category, String key, String defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : tmp;
    }

    @Override
    public String getStringOrDefault(SystemConfigKey key, String defaultValue) {
        return getStringOrDefault(key.getCategory(), key.getKey(), defaultValue);
    }

    @Override
    public int getInt(String category, String key) {
        return Integer.parseInt(getString(category, key));
    }

    @Override
    public int getInt(SystemConfigKey key) {
        return Integer.parseInt(getString(key));
    }

    @Override
    public int getIntOrDefault(String category, String key, int defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : Integer.parseInt(tmp);
    }

    @Override
    public int getIntOrDefault(SystemConfigKey key, int defaultValue) {
        return getIntOrDefault(key.getCategory(), key.getKey(), defaultValue);
    }

    @Override
    public long getLong(String category, String key) {
        return Long.parseLong(getString(category, key));
    }

    @Override
    public long getLongOrDefault(String category, String key, long defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : Long.parseLong(tmp);
    }

    @Override
    public long getLong(SystemConfigKey key) {
        return getLong(key.getCategory(), key.getKey());
    }

    @Override
    public long getLongOrDefault(SystemConfigKey key, long defaultValue) {
        return getLongOrDefault(key.getCategory(), key.getKey(), defaultValue);
    }

    @Override
    public double getDouble(String category, String key) {
        return Double.parseDouble(getString(category, key));
    }

    @Override
    public double getDoubleOrDefault(String category, String key, double defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : Double.parseDouble(tmp);
    }

    @Override
    public double getDouble(SystemConfigKey key) {
        return getDouble(key.getCategory(), key.getKey());
    }

    @Override
    public double getDoubleOrDefault(SystemConfigKey key, double defaultValue) {
        return getDoubleOrDefault(key.getCategory(), key.getKey(), defaultValue);
    }

    @Override
    public JSONObject getJSONObject(String category, String key) {
        return JSONObject.parseObject(getString(category,key));
    }

    @Override
    public JSONObject getJSONObjectOrDefault(String category, String key, JSONObject defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : JSONObject.parseObject(tmp);
    }

    @Override
    public JSONObject getJSONObject(SystemConfigKey key) {
        return getJSONObject(key.getCategory(),key.getKey());
    }

    @Override
    public JSONObject getJSONObject(SystemConfigKey key, JSONObject defaultValue) {
        return getJSONObjectOrDefault(key.getCategory(), key.getKey(), defaultValue);
    }

    @Override
    public JSONArray getJSONArray(String category, String key) {
        return JSONArray.parseArray(getString(category,key));
    }

    @Override
    public JSONArray getJSONArrayOrDefault(String category, String key, JSONArray defaultValue) {
        String tmp = getString(category, key);
        return tmp == null ? defaultValue : JSONArray.parseArray(tmp);
    }

    @Override
    public JSONArray getJSONArray(SystemConfigKey key) {
        return getJSONArray(key.getCategory(),key.getKey());
    }

    @Override
    public JSONArray getJSONArrayOrDefault(SystemConfigKey key, JSONArray defaultValue) {
        return getJSONArrayOrDefault(key.getCategory(), key.getKey() , defaultValue);
    }
}
